package com.sjz.han.industry.utils;

import java.text.DateFormat;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by weiruibin on 2018/6/4.
 */

public class TimeUtils {
    public static String getTodayTime(){
        long time= System.currentTimeMillis();
        Date date=new Date(time);
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }
    //获取年月日日期格式
    public static String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }
    //获取年月日日时分秒期格式
    public static String getTimeAll(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        return format.format(date);
    }
    //获取年月日期格式
    public static String getYEAR_MONTHTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        return format.format(date);
    }
    //获取年格式
    public static String getYEARHTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy");
        return format.format(date);
    }

    /**
     * @param milliseconds 时间戳 单位毫秒
     * @return 参数说明
     * @description 将14位时间戳转化为时间hh-mm/昨天 hh-mm/yyyy-MM-dd hh-mm
     * @date 2015-9-11 下午3:03:13
     */
    public static String convertTime(long milliseconds) {
        Calendar calendar = Calendar.getInstance(Locale.getDefault());
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0,
                0, 0);
        long todayMill = calendar.getTimeInMillis();
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        long yestdayMill = calendar.getTimeInMillis();
        if (milliseconds > todayMill) {
            SimpleDateFormat format = new SimpleDateFormat("HH:mm", Locale.getDefault());
            return format.format(new Date(milliseconds));
        } else if (milliseconds > yestdayMill) {
            SimpleDateFormat format = new SimpleDateFormat("HH:mm", Locale.getDefault());
            return "昨天 " + format.format(new Date(milliseconds));
        } else {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
            return format.format(new Date(milliseconds));
        }
    }


    /**
     * 获取指定日期的时间戳 单位秒
     * yyyy年MM月dd日 HH:mm
     * yyyy年MM月dd日
     */
    public static long tranTimeToMillis(String dateTime, String type) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                type, Locale.getDefault());
        Date date;
        try {
            date = dateFormat.parse(dateTime);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            return cal.getTimeInMillis() / 1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }


    /**
     * 时间戳转化为时间  yyyy-MM-dd
     *
     * @param seconds 单位秒
     */
    public static String millionsToTime(long seconds) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        return format.format(new Date(seconds * 1000));
    }

    /**
     * 时间戳转化为时间  yyyy/MM/dd
     *
     * @param seconds 单位秒
     */
    public static String millionsToTimeSprit(long seconds) {
        DateFormat format = new SimpleDateFormat("yyyy/MM/dd", Locale.getDefault());
        return format.format(new Date(seconds * 1000));
    }

    /**
     * 时间戳转化为时间
     *
     * @param seconds 单位秒
     * @return yyyy-MM-dd  HH:mm:ss
     */
    public static String millionsToDateFormat(long seconds) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss",
                Locale.getDefault());
        return format.format(new Date(seconds * 1000));
    }

    /**
     * 从时间戳获得日期(年)
     */
    public static int millionsGetYear(long seconds) {
        DateFormat format = new SimpleDateFormat("yyyy",
                Locale.getDefault());
        return Integer.parseInt(format.format(new Date(seconds * 1000)));
    }

    /**
     * 从时间戳获得日期(月)
     */
    public static int millionsGetMonth(long seconds) {
        DateFormat format = new SimpleDateFormat("MM",
                Locale.getDefault());
        return Integer.parseInt(format.format(new Date(seconds * 1000)));
    }

    /**
     * 从时间戳获得日期(天)
     */
    public static int millionsGetDay(long seconds) {
        DateFormat format = new SimpleDateFormat("dd",
                Locale.getDefault());
        return Integer.parseInt(format.format(new Date(seconds * 1000)));
    }


    /**
     * 获取当前日期10位时间戳 单位 秒
     */
    public static Long getCurrentDayStampTenNumber() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        Long stamp = 0L;
        try {
            stamp = df.parse(df.format(calendar.getTime())).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return stamp / 1000;
    }

    /**
     * 获取当前系统时间戳 单位 秒
     */
    public static Long getCurrentDayTimestampTenNumber() {
        return System.currentTimeMillis() / 1000;
    }


    /**
     * 将时间戳转换为星期几
     */
    public static String getWeek(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
        return sdf.format(date);
    }


    /**
     * 获得几个月后的日期
     */
    public static String getMonthsLater(int months, long milliseconds) {
        Format f = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(milliseconds * 1000);
        calendar.add(Calendar.MONTH, months);
        return f.format(calendar.getTime());
    }

    /**
     * 获得几个月后的日期
     */
    public static String getDaysLater(int days, long milliseconds) {
        Format f = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(milliseconds * 1000);
        // add方法中的第二个参数n中，正数表示该日期后n天，负数表示该日期的前n天
        calendar.add(Calendar.DATE, days);
        return f.format(calendar.getTime());
    }

    /**
     * 比较time2比time1多几天
     * @return
     */
    public static int differentDays(String time1, String time2)
    {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date2=null ;
        Date date1=null ;
        try
        {
            date2 = format.parse(time2);
            date1 = format.parse(time1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);

        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2)   //同一年
        {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
                if(i%4==0 && i%100!=0 || i%400==0)    //闰年
                {
                    timeDistance += 366;
                }
                else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2-day1) ;
        }
        else    //不同年
        {
            System.out.println("判断day2 - day1 : " + (day2-day1));
            return day2-day1;
        }
    }
}
